import os
import shutil
import gzip
import sys
from netrc import netrc
import requests
from requests.utils import get_netrc_auth
from datetime import datetime, timedelta
from tqdm import tqdm

# satellite = "lageos1"
satellite = "larets"
# satellite = "ajisai"

beginDate = "2024-01-01"
endDate = "2024-02-01"
dataCPFDir = "../dataset/dataCPF/"
yearSet = set()
dataSet = set()
overWrite = False

def get_file_paths(directory):
    file_paths = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            file_paths.append(os.path.normpath(file_path))
    return file_paths

if __name__ == "__main__":
    if overWrite:
        if os.path.exists(dataCPFDir):
            shutil.rmtree(dataCPFDir)
        if not os.path.exists(dataCPFDir):
            os.mkdir(dataCPFDir)

    beginDateTime = datetime.strptime(beginDate, "%Y-%m-%d")
    endDateTime = datetime.strptime(endDate, "%Y-%m-%d")

    fileList = get_file_paths(dataCPFDir)
    for fileName in fileList:
        date = datetime.strptime(fileName.split('_')[2], "%y%m%d")
        beginDateTime = max(beginDateTime, date)
        endDateTime = max(endDateTime, date)

    # 循环直到当前日期超过结束日期
    while beginDateTime <= endDateTime:
        # 将当前日期格式化为所需的格式（年年年月日）
        dateYear = beginDateTime.strftime("%Y")
        dateNow = beginDateTime.strftime("%y%m%d")
        yearSet.add(dateYear)
        dataSet.add(dateNow)

        beginDateTime += timedelta(days=1)

    for date in yearSet:
        check_netrc_url = "https://urs.earthdata.nasa.gov"
        if get_netrc_auth(check_netrc_url) != None:  # 应该返回 ('your_username', 'your_password')

            session = requests.Session()
            url = f"https://cddis.nasa.gov/archive/slr/cpf_predicts_v2/{date}/{satellite}/"

            list_response = session.get(url + "*?list")
            lines = list_response.text.strip().split('\n')
            tgt_files = [line.split() for line in lines]
            result = [item[0] for item in tgt_files if item and item[0].endswith(".hts") and len(item[0].split('_')) > 1 and item[0].split('_')[2] in dataSet]

            t_bar = tqdm(result, total=len(result))
            for tgt_name in t_bar:
                t_bar.set_description(f"{tgt_name.split('_')[2]}")
                fileName = dataCPFDir + tgt_name
                file_response = requests.get(url + tgt_name)
                with open(fileName, 'wb') as fd:
                    for chunk in file_response.iter_content():
                        fd.write(chunk)
                    fd.close()
        else:
            print("没有找到相应的netrc登录信息")
            break
